using Sitecore.Data.Fields;
using SunGard.Corp.Core.Lookups;
using SunGard.Corp.Core.Utilities;

namespace SitecoreFacade.SitecoreItems
{
    public class SitecoreField : ISitecoreField
    {
        private Field field;

        public SitecoreField(Field field)
        {
            this.field = field;
        }

        /// <summary>
        /// Check to see if field is of specified type 
        /// </summary>
        public bool IsOfType(string sitecoreFieldType)
        {
            return (field != null && field.Type.ToLower() == sitecoreFieldType.ToLower());
        }

        /// <summary>
        /// Get string value of field.  For rich text fields, return the inner value (the HTML), otherwise
        /// just return the value.
        /// </summary>
        public string Value
        {
            get
            {
                if (IsOfType(SitecoreFieldType.RichText))
                {
                    HtmlField htmlField = (HtmlField) field;
                    return HTMLParsedToEnsureLinksAndImagesHaveFullyQualifiedPaths(htmlField.InnerField.Value.Trim());
                }
                return (field != null) ? field.Value.Trim() : null;
            }
        }

        private string HTMLParsedToEnsureLinksAndImagesHaveFullyQualifiedPaths(string valueToParse)
        {
            return HTMLUtility.UpdateImageSourceAndHrefTagsToHaveFullyQualifiedPaths(valueToParse);
        }

        /// <summary>
        /// Returns image field attributes for field.  If not an image field, empty strings are
        /// returned for the attributes.
        /// </summary>
        public IImageFieldAttributes ImageFieldAttributes
        {
            get { return new ImageFieldAttributes(field); }
        }

        /// <summary>
        /// Returns multilist field attributes for field.  If not a multilist field, an empty list is
        /// returned for the attributes.
        /// </summary>
        public IMultilistFieldAttributes MultilistFieldAttributes
        {
            get { return new MultilistFieldAttributes(field); }
        }

        /// <summary>
        /// Returns the lookup field attributes for field.  If not a lookupfield, null is returned.
        /// </summary>
        public ILookupFieldAttributes LookupFieldAttributes
        {
            get { return new LookupFieldAttributes(field); }
        }

        /// <summary>
        /// Returns the file field attributes for field.  If not a file field, null is returned.
        /// </summary>
        public IFileFieldAttributes FileFieldAttributes
        {
            get { return new FileFieldAttributes(field); }
        }

        /// <summary>
        /// Returns the file custom attributes for field.  If not a file field, null is returned.
        /// </summary>
        public ICustomFieldAttributes CustomFieldAttributes
        {
            get { return new CustomFieldAttributes(field); }
        }

        /// <summary>
        /// Returns the checkbox field attributes for the field.
        /// </summary>
        public ICheckboxFieldAttributes CheckboxFieldAttributes
        {
            get { return new CheckboxFieldAttributes(field); }
        }
    }
}
